package com.cndw.kungfu.model.sys;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cndw.kungfu.config.ConstantsSystem;
import com.cndw.kungfu.core.CacheDataBase;
import com.cndw.kungfu.dao.AAADao;
import com.cndw.kungfu.domain.DataInfoSys;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.TimeProcesser;

@Component
public class SysManger {
	@Autowired
	private AAADao dao;

	/**
	 * 测试数据库连接
	 */
	public void debugMysql() {
		try {
			dao.debugMysql();
			ConstantsSystem.resetDbErrTimes();
		} catch (Exception e) {
			ConstantsSystem.throwExceptionDB(e);
			// 其他隐藏
		}

	}

	public DataInfoSys loadCacheDataDb(String key) {
		try {
			return dao.loadCacheData(key);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public boolean saveToDb(CacheDataBase cacheDataBase, String sysData) {
		try {
			int syncDBTime = TimeProcesser.getUnixTime();
			if (cacheDataBase.getSyncDBTime() == 0) {
				dao.insertCacheData(cacheDataBase.getKey(), sysData, syncDBTime);
				LogUtil.getMain().debug(" first sync "+cacheDataBase.getClass().getSimpleName()+"  "+cacheDataBase.getKey());
			} else {
				dao.saveCacheData(cacheDataBase.getKey(), sysData, syncDBTime);
			}
			cacheDataBase.setSyncDBTime(syncDBTime);
			//LogBase.loger.debug("xxxxx");
		} catch (Exception e) {
			ConstantsSystem.throwExceptionDB(e);
			return false;
		}
		return true;
	}

}
